package com.yunqiao.service.api.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.Query;
import com.yunqiao.service.api.RegionService;
import com.yunqiao.service.api.entity.RegionEntity;
import com.yunqiao.service.api.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("regionService")
public class RegionServiceImpl extends ServiceImpl<RegionMapper, RegionEntity> implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //各种查询条件的过滤,在此进行
        QueryWrapper<RegionEntity> queryWrapper = new QueryWrapper<>();

        IPage<RegionEntity> page = this.page(new Query<RegionEntity>(params).getPage(),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<Object> getLocation(Integer pid) {
        List<Object> list = new ArrayList<>();

        List<RegionEntity> regionEntityList = regionMapper.selectList(new QueryWrapper<RegionEntity>().eq("pid", pid));
        Dict dict = Dict.create().set("label", "").set("value", "");
        list.add(dict);
        for (RegionEntity regionEntity : regionEntityList) {
            list.add(Dict.create().set("label", regionEntity.getName()).set("value", regionEntity.getId()));
        }
        return list;
    }

}
